package com.guaoran.algorithm.common;

import com.guaoran.algorithm.entity.Node;
import com.guaoran.algorithm.entity.NodeList;
import com.guaoran.algorithm.entity.TreeNode;

import java.util.*;

/**
 * @Auth gucheng
 * @Desc 通用工具
 * @Date 2020/12/17 下午2:44
 */
public class CommonUtil {

    /**
     * 组装集合
     * @param length
     * @return
     */
    public static Node createNode(int length){
        return createNodeList(length).head;
    }

    /**
     * 组装带随机指针
     * @param length
     * @return
     */
    public static Node createNodeWithRandom(int length){
        if (length <=1) length = 5;
        length --;
        NodeList nodeList = createNodeList(length);

        Node node = new Node(length+1);
        nodeList.addNode(node);
        nodeList.head.random = node;
        return nodeList.head;
    }


    private static NodeList createNodeList(int length){
        if (length <=1) length = 5;
        NodeList<Integer> nodeList = new NodeList<>();
        for (int i = 1; i <=length; i++) {
            nodeList.add(i);
        }
        return nodeList;
    }

    public static void main(String[] args) {
        System.out.println(createNodeList(6));
        Node nodeListWithRandom = createNodeWithRandom(6);
        System.out.println(nodeListWithRandom);
        Integer[] arr = {null,null,1,2,3,null,null,4,5,null,null};
        TreeNode treeNode = createTreeNode(arr);
        System.out.println(readTreeNode(treeNode));
    }


    public static TreeNode createTreeNode(Integer[] arr){
        if(arr == null){
            return null;
        }
        // 去除前后包含 null 的元素
        int start = 0;
        int end = arr.length -1;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                start = i;
                break;
            }
        }
        for (int i = arr.length-1; i >=0 ; i--) {
            if(arr[i] != null){
                end = i;
                break;
            }
        }
        Integer[] copy = Arrays.copyOfRange(arr, start, end+1);

        List<TreeNode> queue = new ArrayList<>();
        TreeNode root = new TreeNode(copy[0]);
        queue.add(root);
        boolean isLeft = true;
        int index = 0;
        for (int i = 1; i < copy.length; i++) {
            if(copy[i] != null){
                TreeNode node = new TreeNode(copy[i]);
                if(isLeft){
                    queue.get(index).left = node;
                } else {
                    queue.get(index).right = node;
                }
                queue.add(node);
            }
            if(!isLeft){
                index ++;
            }
            isLeft = !isLeft;
        }
        return root;
    }


    public static List<TreeNode> readTreeNode(TreeNode root){
        List<TreeNode> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        list.add(root);
        for (int i = 0; i < list.size(); i++) {
            TreeNode treeNode = list.get(i);
            if(treeNode == null){
                continue;
            }
            list.add(treeNode.left);
            list.add(treeNode.right);
        }

        while(list.get(list.size()-1) == null){
            list.remove(list.size()-1);
        }

        return list;
    }
}
